/**
 * All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
 *
 *MpmProjectOverdueRecordController
 * 销售项目超期记录
 *
 * 修改纪录
 * 2022-04-21 版本：1.0 houyuhang 创建。
 * @version 版本：1.0
 * @author 作者：houyuhang
 * 创建日期：2022-04-21
 */

package com.topscomm.mpm.controller;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.topscomm.basic.exception.BusinessException;
import com.topscomm.pub.util.*;
import org.springframework.web.bind.annotation.*;

import com.topscomm.mpm.controller.auto.MpmProjectOverdueRecordControllerAuto;
import com.topscomm.pub.vo.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: houyuhang
 * @date: 2022-04-21
 * @description:销售项目超期记录Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectOverdueRecord")
public class MpmProjectOverdueRecordController extends MpmProjectOverdueRecordControllerAuto implements IMpmProjectOverdueRecordController {
	/**
	 * @author: houyuhang
	 * @date: 2022-04-21
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: houyuhang
	 * @date: 2022-04-21
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-04-22
	 * @param paraMap
	 * @return
	 * @description:获取超期统计信息
	 * @modify:
	 */
	@GetMapping("/loadOverDueSummary")
	public ResponseResult<Object> loadOverDueSummary(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			List<Map<String, Object>> res = this.service.loadOverDueSummary(paraMap);
			Map<String, Object> resMap = new HashMap<>();
			resMap.put("dataList", res);
			return ResponseResult.ok(resMap);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
	/**
	 * @description:导出超期统计Excel
	 * @author: houyuhang
	 * @date: 2022-05-06
	 * @modify:
	 */
	@PostMapping(value = "/overdueExportExcel")
	public void overdueExportExcel(@RequestBody(required = false) Map<String, Object> paraMap, HttpServletRequest request, HttpServletResponse response) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			List<Map<String, Object>> dataList = this.service.loadOverDueSummary(paraMap);
			String[] colArray = ConvertUtil.convertToString(paraMap.get("cols")).split(",");
			InputStream inStream = OfficeOperateUtil.exportExeclXlsx(dataList, colArray);
			String fileName = "超期统计.xlsx";
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			response.setHeader("FileName", URLEncoder.encode(fileName, "utf-8"));
			// 暴露自定义的首部,供页面资源使用
			response.setHeader("Access-Control-Expose-Headers", "FileName");
			OutputStream outStream = response.getOutputStream();
			byte[] bytes = new byte[2048];
			int len;
			while ((len = inStream.read(bytes)) != -1) {
				outStream.write(bytes, 0, len);
			}
			inStream.close();
		} catch (Exception ex) {
			throw new BusinessException(ex.getMessage(), true);
		}
	}
}

